Namespace ShapeLibrary
    '''
    ''' This is an adapter class we have as part of our
    ''' shape library.  Shapes themselves are generic and
    ''' can be used on both windows forms and ASP.Net forms.
    ''' To keep then generic, they don't have any knowledge of
    ''' either UI library.  So we create adapter classes that 
    ''' can bind them up.  This class can be easily replaced
    ''' by writing some hookup code, but we're using it to
    ''' demonstrate how to write custom serialization code
    ''' in the designers.
    '''
    <DesignerSerializer(GetType(ShapeLibrary.Design.WindowsFormsAdapterCodeDomSerializer), GetType(CodeDomSerializer))> _
    Friend Class WindowsFormsAdapter
        Private m_container As ShapeContainer
        Private m_control As Control

        '''
        ''' Connects a shape container to a windows forms control.
        '''
        Friend Sub Connect(ByVal c As Control, ByVal container As ShapeContainer)
            m_control = c
            m_container = container
            AddHandler m_control.Paint, New PaintEventHandler(AddressOf OnControlPaint)
        End Sub

        '''
        ''' Disconnects a shape container from a windows forms control.
        ''' If no control was connected, this does nothing.
        '''
        Friend Sub Disconnect()
            If Not (m_control Is Nothing) Then
                RemoveHandler m_control.Paint, New PaintEventHandler(AddressOf OnControlPaint)
                m_control = Nothing
            End If
        End Sub

        '''
        ''' Private event handler that listens to the control's
        ''' Paint event.  Here we just call Draw on the shape
        ''' container.
        '''
        Private Sub OnControlPaint(ByVal sender As Object, ByVal pe As PaintEventArgs)
            m_container.Draw(pe.Graphics)
        End Sub
    End Class
End Namespace